var EpgTrailer = Class.create(EpgWidget, {
	initialize : function($super, id, opts, links) {
		$super(id, opts, links);
		this.currentIdx = 0;// 选中的内容对应索引
		this.mediaCodeStr = "mediaCode_";
		this.currentPage = 1;// 选中内容所在页码
		this.left = opts.left;
		this.top = opts.top;
		this.width = opts.width;
		this.height = opts.height;
		this.hasText = opts.hasText;
		this.titleNum = opts.titleNum;
		this.playUrl = opts.playUrl;
		this.playType = opts.playType;
		// this.trailerId = "trailerScript";
		this.init();
	},
	getPlayJson : function(playUrl, mediaCode) {
		var json = '[{mediaUrl:"' + playUrl + '",';
		json += 'mediaCode:"' + mediaCode + '",';
		json += 'mediaType:2,';
		json += 'audioType:1,';
		json += 'videoType:1,';
		json += 'streamType:1,';
		json += 'drmType:1,';
		json += 'fingerPrint:0,';
		json += 'copyProtection:1,';
		json += 'allowTrickmode:1,'; // 允许快进快退
		json += 'startTime:0,';
		json += 'endTime:20000,';
		json += 'entryID:"jsonentry1"}]';
		return json;
	},
	init : function() {
		if (typeof(MediaPlayer) != "undefined") {
			this.mp = new MediaPlayer();
			this.mp.setVideoDisplayArea(this.left, this.top, this.width,
					this.height);
			this.mp.setVideoDisplayMode(0);
			this.mp.setAllowTrickmodeFlag(0);
			this.mp.setNativeUIFlag(0);
			this.mp.setMuteUIFlag(0);
		}
	},
	//通过url（rtsp)进行播放
	playTrailerByUrl : function(url) {
		this.mp.setSingleMedia(this.getPlayJson(url, this.mediaCode));
		this.mp.refreshVideoDisplay();
		this.mp.playFromStart();
	},
	playTrailerChannel : function(channelNum) {
		this.mp.leaveChannel();
		this.mp.joinChannel(channelNum);
		this.mp.setVideoDisplayMode(0);
		this.mp.setVideoDisplayArea(this.left, this.top, this.width,
				this.height);
		this.mp.refreshVideoDisplay();
		this.mp.play();
	},
	//通过url（rtsp)进行播放
	addScriptFile : function(url, handler) {
		if (document && document.createElement) {
			var new_element = document.createElement("script");
			new_element.setAttribute("type", "text/javascript");
			new_element.setAttribute("onload", handler);
			new_element.setAttribute("src", url);
			if (document.getElementsByTagName
					&& document.getElementsByTagName('html')
					&& document.getElementsByTagName('html').item(0)) {
				var head = document.getElementsByTagName('html').item(0);
				if (head.appendChild) {
					head.appendChild(new_element);
				}
			}
		}
	},
	playTrailer : function(mediaCode, index, serverAddr) {
		//当前mediaCode
		this.mediaCode = mediaCode;
		//当前索引
		this.currentIdx = index;
		//播放琏接（rtsp)接口请求url
		// var trailerUrl = this.getTrailerPlayUrl(mediaCode);
		// this.addScriptFile(trailerUrl,"");

		var url = serverAddr + "/sites/hdvod/play/play_vod_trailer.jsp?left=" + this.left
				+ "&top=" + this.top + "&width=" + this.width + "&height="
				+ this.height;
		var codeParam = "&foreignId=" + this.mediaCode;
		if (this.playType == "channel") {
			url = serverAddr + "/sites/hdvod/play/play_channel_trailer.jsp?";
			codeParam = "&channelIndex=" + this.mediaCode;
		}
		this.addScriptFile(url + codeParam, "");
	},
	removeTrailer : function() {
		var elem = document.getElementById(this.trailerId);
		if (elem) {
			elem.parentNode.removeChild(elem);
		}
	},
	//获取播放琏接（rtsp)接口请求url
	getTrailerPlayUrl : function(mediaCode) {
		return this.playUrl
				+ "?info=<action>get_playurl</action><play_type>1</play_type><foreigncode>"
				+ mediaCode
				+ "</foreigncode><back_type>5</back_type><back_param1>getPlayUrl</back_param1>";
	},
	stop : function() {
		this.mp.leaveChannel();
		this.mp.stop();
	},
	destroy : function() {
		this.stop();
	},
	adjustVolume : function(vol) {
		if (this.mp != null) {
			try {
				var volume = this.mp.getVolume();
				volume += vol;
				if (volume > 100) {
					volume = 100;
				} else if (volume < 0) {
					volume = 0;
				}
				this.mp.setVolume(volume);
			} catch (e) {
			}
		}
	},
	adjustMute : function() {
		if (this.mp != null) {
			try {
				var flag = this.mp.getMuteFlag() > 0 ? 0 : 1;
				this.mp.setMuteFlag(flag);
			} catch (e) {
			}
		}
	},
	//播放下一条
	playNext : function() {
		var eventJson = Utility.getEvent();
		eval("eventObj = " + eventJson);
		var typeStr = eventObj.type.toUpperCase();
		if (typeStr == "EVENT_MEDIA_END" || typeStr == "EVENT_MEDIA_ERROR") {
			if (trailerPlay.currentPage != contentWidget.opts.pageInfo.pageNo) {// (缈婚〉鍚�)灏忕獥鍙ｅ唴瀹逛笉鍦ㄥ綋鍓嶉〉闈㈠唴
				//更改trailerWidget的属性
				this.currentIdx = 0;
				//更改trailer的标记属性
				trailerPlay.currentId = 0;
				trailerPlay.currentPage = contentWidget.opts.pageInfo.pageNo;
				this.playNextAction(this.currentIdx);
			} else if (this.currentIdx < trailerPlay.lastIdx) {
				var idx = parseInt(this.currentIdx) + 1;
				this.playNextAction(idx);
			} else if (this.currentIdx == trailerPlay.lastIdx) {
				if (trailerPlay.currentPage == contentWidget.opts.pageInfo.pageCount) {
					//最后一条VOD播完后,从头开始播
					contentWidget.ajax(1);
					var link = epgPage.findLink("movie_link_0");
					if (link) {
						epgPage.switchFocus(link);
					}
					trailerPlay.currentPage = 1;
				} else {
					contentWidget.pageDown();
					trailerPlay.currentPage = trailerPlay.currentPage + 1;
				}
				//更改trailer的标记属性
				trailerPlay.currentId = 0;
				//更改trailerWidget的属性
				this.currentIdx = 0;
				this.playNextAction(0);
			} else { //循环播放
				playTrailer(this.mediaCode, 0);
			}
		}
	},
	//播放下一条所需的操作
	playNextAction : function(idx) {
		var el = document.getElementById(this.mediaCodeStr + '' + idx);
		var mediaCode = el.value;
		//聚焦
		var link = epgPage.findLink("movie_link_" + idx);
		if (link) {
			epgPage.switchFocus(link);
		}
		//更改背景
		trailerPlay.playNextStyle(idx);
		//调用播放
		this.playTrailer(mediaCode, idx);
	},
	onkeypress : function(ev) {
		keycode = ev.which;
		switch (keycode) {
			case KEY_ENUM.KEY_VOL_UP :
				this.adjustVolume(1);
				return true;
			case KEY_ENUM.KEY_VOL_DOWN :
				this.adjustVolume(-1);
				return true;
			case KEY_ENUM.KEY_VOL_MUTE :
				this.adjustMute();
				return true;
			case KEY_ENUM.KEY_VOD_OVER :
				this.playNext();
				return true;
		}
		return false;
	}
});
function f(json) {
	alert("fff:" + json.url);
}
//播放回调函数
function getPlayUrl(data) {
	var playUrl = data;
	trailerWidget.playTrailerByUrl(playUrl);
}